package com.dxy.md.user.service;

import com.dxy.md.core.DataSource;
import com.dxy.md.user.entity.User;
import com.dxy.md.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    @DataSource("master")
    public void save(User user) {
        userMapper.insertUser(user);
    }

    @Override
    @DataSource("slave")
    public User findById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    @Transactional
    @DataSource("slave")
    public void testTransaction() {
        User user = new User().setName("xxx");
        userMapper.insertUser(user);
        throw new RuntimeException("测试事务报错");
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }
}
